package com.base.controller;

import com.base.po.voip_coupon.CouponBrand;
import com.base.service.COUPON.CouponBrandService;
import com.base.service.COUPON.CouponCategoryService;
import com.base.utils.Page;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 365优惠券品牌管理服务端
 *
 * @author K
 */
@Controller
@RequestMapping("/admin")
public class Index_CouponBrand {
    @Autowired
    private CouponCategoryService couponCategoryService;
    @Autowired
    private CouponBrandService couponBrandService; // 分类信息

    /**
     * 商品品牌首页
     *
     * @param request       参数
     * @param topId         分类Id
     * @param currentPageNo 当前页
     * @return ModelAndView
     * @throws Exception
     */
    @RequestMapping("/couponBrandIndex")
    public ModelAndView couponBrandIndex(HttpServletRequest request, String topId, Integer currentPageNo) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        page.setCurrentPageNo(currentPageNo); // 设置当前页
        page.setRecordTotal(couponBrandService.countAll(userid, topId, null)); // 设置总条数
        List<CouponBrand> brands = couponBrandService.selectAll(page, userid, topId, null, 1); // 分类内容集合
        for (CouponBrand brand : brands){
            brand.setTopid(couponCategoryService.selectByPrimaryKey(brand.getTopid()).getCategory());
        }
        map.put("brands", brands); // 对象
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/index_CouponBrand", map);
    }

    // 跳转添加品牌信息
    @RequestMapping("/addCouponBrand")
    public ModelAndView addCouponBrand(HttpServletRequest request) throws Exception {
        String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
        if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
        Map<String, Object> map = new HashedMap();
        map.put("categories", couponCategoryService.selectAll(null, userid, 1));
        return new ModelAndView("jsp/add_CouponBrand", map);
    }

    // 跳转修改品牌信息
    @RequestMapping("/updateCouponBrand")
    public ModelAndView updateCouponBrand(String id) throws Exception {
        Map<String, Object> map = new HashMap<>(); // 存储数据对象
        map.put("brand", couponCategoryService.selectByPrimaryKey(id)); // 对象
        return new ModelAndView("jsp/update_CouponBrand", map);
    }
}
